package com.htjc.integral.test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import com.htjc.integral.common.util.EncryptUtils;

/*
 * 伪造数据的工具类
 * 
 * */
public class DataFactory {

	public static void main(String[] args) {
		getParams01();//单个查询参数伪造
//		getParams02();//单个增减数据伪造
//		getMingwen();//解密uid
//		getDeductionIntegralParams();//批量积分扣减数据伪造
	}
	
	// 查询参数伪造
	public static void getParams01() {
		String CLIENTID = "42";

		String uid = "44f90082e9f74d979ab9bc2bd18c11a6";// 加密前的数据
		String memberId = EncryptUtils.encodeDataBase64(uid);
		System.out.println("加密后的用户id（uid）：" + memberId);
		
		String KEY = "018980296a626116350c7bb99ff87377";
		
		String timestamp = "1530670938715";
//		String timestamp = getTimeStemp();
		System.out.println("生成的随机字符串(timestamp)：" + timestamp);

		// 01.用户授权登录接口和积分查询接口
		String str01 = CLIENTID + "#" + memberId + "_" + KEY + "#" + timestamp;
		
		System.out.println("加密前字符串(sign)：" + str01);

		String sign01 = toMD5(str01);
		System.out.println("MD5加密字符串(sign)：" + sign01);
		System.out.println("http://localhost:20034/vip/service/userAuthorizeLogin?clientId="+CLIENTID+"&timestamp="+timestamp+"&uid="+memberId+"&sign="+sign01);
		System.out.println("=================================================");

		String rtmOrderNum = "XPCS3024201";
		
		// 02.订单状态查询接口
		 String str02 = CLIENTID + "#" + rtmOrderNum + "_" + KEY + "#" + timestamp;
		// String str = "42#XPCS3024201_018980296a626116350c7bb99ff87377#1530670938715";

		System.out.println("加密前字符串(sign)：" + str02);
		String sign02 = toMD5(str02);
		System.out.println("MD5加密字符串(sign)：" + sign02);
		System.out.println("http://localhost:20034/vip/service/queryOrderStatus?clientId="+ CLIENTID +"&timestamp="+timestamp+"&rtmOrderNum="+rtmOrderNum+"&sign="+ sign02);
		
		//03.积分查询接口
		String str03 = CLIENTID + "#" + memberId + "_" + KEY + "#" + timestamp;
		String description = "积分查询接口";
		System.out.println("http://localhost:20034/vip/service/queryIntegral?uid="+memberId+"&clientId="+CLIENTID+"&timestamp="+timestamp+"&description="+description+"&sign="+str03);
		

	}
	
	//增减数据伪造
	public static void getParams02() {
		String CLIENTID = "42";//客户id
		
		String uid = "44f90082e9f74d979ab9bc2bd18c11a6";// 加密前的数据
		String memberId = null;//用户id
		memberId = EncryptUtils.encodeDataBase64(uid);
		System.out.println("加密后的用户id（memberId）：" + memberId);
		
		String rtmOrderNum = null;//订单编号
		rtmOrderNum = "XPCS1000088";
		System.out.println("订单编号rtmOrderNum：" + rtmOrderNum);
		
		String credits = null;//扣减积分
		String totalNum = null;
		credits = "210";
		totalNum = credits;
		System.out.println("扣减积分totalNum：" + totalNum);
		
		String KEY = "018980296a626116350c7bb99ff87377";//秘钥
		System.out.println("对接秘钥KEY：" + KEY);
		
		
		String timestamp = "1530670938715";
//		String timestamp = getTimeStemp();
		System.out.println("生成的随机字符串(timestamp)：" + timestamp);
		
		String str = CLIENTID + "#" + memberId + "_" + rtmOrderNum + "_" + totalNum + "#" + KEY + "#" + timestamp;
		System.out.println("加密前字符串(sign)：" + str);

		String sign = toMD5(str);
		System.out.println("MD5加密字符串(sign)：" + sign);
		System.out.println("请求地址："+"http://localhost:20034/vip/service/deductionIntegral?uid="+memberId+"&credits=210&clientId=42&timestamp=1530670938715&description=wpz&rtmOrderNum="+rtmOrderNum+"&pointType=2&sign="+sign);
		System.err.println("说明：变更哪个参数就修改哪个参数！！！");
	}
	
	public static void getDeductionIntegralParams() {
		System.out.println("批量积分扣减数据伪造:");
		for(int i =1000100;i<1000200;i++) {
			String CLIENTID = "42";// 客户id

			String uid = "44f90082e9f74d979ab9bc2bd18c11a6";// 加密前的数据
			String memberId = EncryptUtils.encodeDataBase64(uid);// 加密后的数据：NDRmOTAwODJlOWY3NGQ5NzlhYjliYzJiZDE4YzExYTY=

			String rtmOrderNum = null;// 订单编号
			String _rtmOrderNum = "XPCS";
			int rtmOrderNum_ = i;
			rtmOrderNum = _rtmOrderNum + rtmOrderNum_;
//			System.out.println("订单编号rtmOrderNum：" + rtmOrderNum);

			String credits = null;// 扣减积分
			String totalNum = null;
			credits = "210";
			totalNum = credits;

			String KEY = "018980296a626116350c7bb99ff87377";// 秘钥

			String timestamp = "1530670938715";

			String str = CLIENTID + "#" + memberId + "_" + rtmOrderNum + "_" + totalNum + "#" + KEY + "#" + timestamp;
//			System.out.println("加密前字符串(sign)：" + str);

			String sign = toMD5(str);
//			System.out.println("MD5加密字符串(sign)：" + sign);
			System.out.println(rtmOrderNum + "," + sign);
		}
	}
	
	public static void getMingwen() {
		String _memberId = "NzJhYTJmOWNjZDhmNDVmOGJlZjM3ZjQ5YzQ3MmY1NGE=";
		String memberId = EncryptUtils.decodeDataBase64(_memberId);
		System.out.println("解密后的数据memberId：" + memberId);//72aa2f9ccd8f45f8bef37f49c472f54a
	}

	public static String getTimeStemp() {
		long timeStamp = new Date().getTime();
		String timestampStr = String.valueOf(timeStamp);
		return timestampStr;
	}

	// MD5加密
	private static String toMD5(String signature) {
		MessageDigest mDigest = null;
		try {
			mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(signature.getBytes());
			return encodeHex(mDigest.digest());
		} catch (NoSuchAlgorithmException e) {
		}
		return null;
	}

	private static String encodeHex(byte[] bytes) {
		StringBuffer buffer = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10)
				buffer.append("0");
			buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buffer.toString();
	}
}
